package cn.edu.tsinghua.cic.ccss.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.forten.core.ro.DtoResultWithPageInfo;
import org.forten.dataio.excel.ExcelUtil;
import org.forten.dataio.excel.exporter.Excel2003Exporter;
import org.forten.dataio.excel.exporter.Excel2007Exporter;
import org.forten.dataio.excel.exporter.ExcelExporter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.edu.tsinghua.cic.ccss.bo.CourseStatisticsBo;
import cn.edu.tsinghua.cic.ccss.dto.CourseStatisticsDto;
import cn.edu.tsinghua.cic.ccss.dto.CourseStatisticsDto4Export;
import cn.edu.tsinghua.cic.ccss.dto.CourseTotalStatisticsDto;
import cn.edu.tsinghua.cic.ccss.qo.CourseStatisticsQo;

@Controller
@Scope("prototype")
@RequestMapping("statistics")
@SessionAttributes("courseStatisticsQo")
public class CourseStatisticsAction {
	
	@Resource(name = "courseStatisticsQo")
	private CourseStatisticsQo courseStatisticsQo;
	@Resource(name = "courseStatisticsBo")
	private CourseStatisticsBo courseStatisticsBo;
	
	@RequestMapping("gotoCourseStatisticsList")
	public String gotoCourseStatisticsList(Model model){
		model.addAttribute("courseStatisticsQo", courseStatisticsQo);
		return "forward:showCourseStatisticsList.jspx";
	}
	
	@RequestMapping("showCourseStatisticsList")
	public String showCourseStatisticsList(Model model,
			@ModelAttribute("courseStatisticsQo")CourseStatisticsQo courseStatisticsQo,
			@RequestParam(defaultValue = "1") int pageNo){
		DtoResultWithPageInfo<CourseStatisticsDto> dtoList = courseStatisticsBo.
				queryCourseStatisticsByQo(courseStatisticsQo,pageNo);
		if(dtoList.getResults().size() != 0){
			CourseTotalStatisticsDto totalDto = courseStatisticsBo.
					queryCourseTotalStatistics(courseStatisticsQo);
			model.addAttribute("totalDto", totalDto);
		} else {
			model.addAttribute("totalDto", new CourseTotalStatisticsDto());
		}
		model.addAttribute("dto", dtoList.getResults());
		model.addAttribute("page", dtoList.getPageInfo());
		model.addAttribute("courseStatisticsQo", courseStatisticsQo);
		return "statistics/courseStatistics";
	}
	
	@RequestMapping("export2003")
	public void export2003(HttpServletResponse response,
			HttpServletRequest request,
			@ModelAttribute("courseStatisticsQo") CourseStatisticsQo courseStatisticsQo){
		List<CourseStatisticsDto4Export> result = courseStatisticsBo.export(courseStatisticsQo);
		ExcelExporter export = new Excel2003Exporter();
		Workbook wb = export.export(result);
		if (wb == null) {
			RequestDispatcher view = request
					.getRequestDispatcher("../WEB-INF/pages/error-msg.jsp");
			try {
				List<String> errMsg = new ArrayList<String>();
				errMsg.add("导出数据结果集为空");
				request.setAttribute("errMsg", errMsg);
				request.setAttribute("url", "gotoCourseStatisticsList.jspx");
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			CourseTotalStatisticsDto totalDto = courseStatisticsBo.
					queryCourseTotalStatistics(courseStatisticsQo);
			courseStatisticsBo.insertTotalNumsIntoExcel(wb, totalDto);
			ExcelUtil.downloadFile(response, wb,
					"CourseStatistics" + export.getPostfix());
		}
	}
	
	@RequestMapping("export2007")
	public void export2007(HttpServletResponse response,
			HttpServletRequest request,
			@ModelAttribute("courseStatisticsQo") CourseStatisticsQo courseStatisticsQo){
		List<CourseStatisticsDto4Export> result = courseStatisticsBo.export(courseStatisticsQo);
		ExcelExporter export = new Excel2007Exporter();
		Workbook wb = export.export(result);
		if (wb == null) {
			RequestDispatcher view = request
					.getRequestDispatcher("../WEB-INF/pages/error-msg.jsp");
			try {
				List<String> errMsg = new ArrayList<String>();
				errMsg.add("导出数据结果集为空");
				request.setAttribute("errMsg", errMsg);
				request.setAttribute("url", "gotoCourseStatisticsList.jspx");
				view.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			CourseTotalStatisticsDto totalDto = courseStatisticsBo.
					queryCourseTotalStatistics(courseStatisticsQo);
			courseStatisticsBo.insertTotalNumsIntoExcel(wb, totalDto);
			ExcelUtil.downloadFile(response, wb,
					"CourseStatistics" + export.getPostfix());
		}
	}
}
